<%--
  Created by IntelliJ IDEA.
  User: Gao
  Date: 2022/5/25
  Time: 8:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="header.jsp"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form id="formId" class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="text" name="password" autocomplete="off"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="phone" autocomplete="off"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" pane="">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <input type="radio" name="type" value="0" title="管理员" checked="">
                <input type="radio" name="type" value="1" title="普通用户">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图片上传</label>
            <div class="layui-upload layui-input-inline">
                <div class="layui-upload-list">
                    <input type="hidden" name="image"/>
                    <img width="150px" height="150px" class="layui-upload-img" id="image">
                    <p id="demoText"></p>
                </div>
                <button type="button" class="layui-btn" id="uploadBtn">上传图片</button>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" onclick="submitForm()" >添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script>
        layui.use(['form', 'upload'], function(){
            var form = layui.form;
            var upload = layui.upload;

            //常规使用 - 普通图片上传
            var uploadInst = upload.render({
                elem: '#uploadBtn'
                ,url: '/upload/uploadImage' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#image').attr('src', result); //图片链接（base64）
                    });
                }
                ,done: function(jsonResult){
                    if(jsonResult.code == 0){
                        var fileName = jsonResult.data;
                        $('input[name="image"]').val(fileName);
                    }


                    //上传成功的一些操作
                    //……
                    $('#demoText').html(''); //置空上传失败的状态
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });
        });



        function submitForm() {
            $.post(
                '/user/add',
                $('#formId').serialize(), // {'name':'zhansgan','age':23}
                function (jsonResult) {
                    if (jsonResult.ok) {
                        // 弹出成功提示消息，消息消失时候关闭这个弹出框
                        layer.msg(
                            jsonResult.msg,
                            {icon : 1, time : 2000},
                            function() {// msg消失之后触发的函数
                                // 获得当前弹出框的index
                                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                parent.location.reload();//刷新父级页面
                            }
                        );
                    }
                },
                'json'
            );
        }
    </script>
</body>
</html>
